package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
//套菜实现类
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    //引入套餐菜品关系信息接口
    @Autowired
    SetmealDishService setmealDishService;

    //执行保存套餐信息，保存套餐和套餐菜品关系
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息
        this.save(setmealDto);
        //保存套餐菜品关系信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((tim) -> {
            tim.setSetmealId(setmealDto.getId());
            return tim;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
    //删除
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        //创建和ids相同的条件
        slqw.in(Setmeal::getId,ids);
        //创建状态为1启用的条件
        slqw.eq(Setmeal::getStatus,1);
        //查询条数
        int count = this.count(slqw);
        //查询出来的条数只要有0条以上说明存在启用的
        if (count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，则删除套餐数据
        this.removeByIds(ids);
        //在删除套餐菜品关系表
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(SetmealDish::getSetmealId,ids);
        //删除
       setmealDishService.remove(sdlqw);
    }



    //修改
    @Override
    public void updateWithFlavor(SetmealDto setmealDto) {
        //更新dish表基本信息
        this.updateById(setmealDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> dlqw = new LambdaQueryWrapper<>();
        //设置条件根据dish的id来查询相关的口味
        dlqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //删除
        setmealDishService.remove(dlqw);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        Long setmealId = setmealDto.getId();
        //菜品口味的添加
        List<SetmealDish> flavors = setmealDto.getSetmealDishes();
        for (SetmealDish flavor : flavors) {
            flavor.setSetmealId(setmealId);
        }
        //执行添加
        setmealDishService.saveBatch(flavors);
    }
    //根据id查询
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> dlqw = new LambdaQueryWrapper<>();
        dlqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> flavors = setmealDishService.list(dlqw);
        setmealDto.setSetmealDishes(flavors);
        return setmealDto;
    }


}
